package diona.model

import com.restfb.types.Post
import com.restfb.{Parameter, DefaultFacebookClient}
import scala.collection.JavaConversions._
import org.joda.time.DateTime
import diona.search.PostInfo

/**
 * User: anton
 * Date: 26.06.11 12:44
 */
object FacebookSearch
{
  def search(queryText: String): List[PostInfo] =
  {
    if (queryText.trim.length == 0) {
      return Nil
    }

    val fb = new DefaultFacebookClient()

    fb.fetchConnection("search", classOf[Post], Parameter.`with`("q", queryText),
      Parameter.`with`("type", "post")).getData.toList.map(createPostInfo)
  }

  def createPostInfo(post: Post) : PostInfo = {
    new PostInfo(url = post.getLink, tstamp = new DateTime(post.getCreatedTime), isComment = false,
      snippet = Option(post.getMessage), author = Some(post.getFrom.getName), title = Option(post.getDescription))
  }
}